﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;
using BO = Viewer.BusinessObject.BusinessEntities;
using System.IO;
using System.Collections.Generic;
namespace Viewer.NhanSu.UserControl
{
    public partial class UCNhanVien : System.Web.UI.UserControl
    {
        #region Khai báo
        private List<NSTrangThaiLamViec> trangThaiObject;
        private List<NSQuaTrinhGianDoanBaoHiem> qtBaoHiemObject;
        private List<NSNghiPhep> nghiPhepObject;
        private List<NSDaoTao> dtObject;
        private List<NSQuanHeGiaDinh> quanHeObject;
        private List<NSQuanHeGiaDinh> quanHeBanThanObject;
        private List<NSQuaTrinhCongTac> qtctObject;
        private List<NSQuaTrinhCongTacDang> qtctDangObject;
        private List<NSNhanXet> nhanXetObject;
        private List<NSNgoaiNgu> ngoaiNguObject;
        private List<NSKiLuat> kiLuatObject;
        private List<NSKhenThuong> khenThuongObject;
        private List<NSHopDongLaoDong> hdldObject;
        private List<NSPhuCapChucVu> pccvObject;
        private List<NSPhuCapKhac> pckObject;
        private List<NSNgachBacLuong> nblObject;
        private List<NSNgheNghiep> ngheNghiepObject;
        private List<NSTrinhDoChuyenMon> trinhDoChuyenMonObject;
        private List<NSBangCap> bangCapObject;
        private List<NSLiLuanChinhTri> liLuanObject;
        private List<NSQuanLiNhaNuoc> quanLiNNObject;
        private List<NSTrinhDoTinHoc> trinhDoTinHocObject;

        NSTrinhDoTinHoc trinhDoTinHoc = new NSTrinhDoTinHoc();
        NSQuanLiNhaNuoc quanLiNN = new NSQuanLiNhaNuoc();
        NSLiLuanChinhTri liLuan = new NSLiLuanChinhTri();
        NSBangCap bangCap = new NSBangCap();
        NSTrinhDoChuyenMon trinhDoChuyenMon = new NSTrinhDoChuyenMon();
        NSTrangThaiLamViec trangThai = new NSTrangThaiLamViec();
        NSQuaTrinhGianDoanBaoHiem baoHiem = new NSQuaTrinhGianDoanBaoHiem();
        NSNghiPhep nghiPhep = new NSNghiPhep();
        NSNgheNghiep ngheNghiep = new NSNgheNghiep();
        NSQuanHeGiaDinh quanHe = new NSQuanHeGiaDinh();
        NSDaoTao dt = new NSDaoTao();
        NSQuaTrinhCongTac qtct = new NSQuaTrinhCongTac();
        NSQuaTrinhCongTacDang quaTrinhCongTacDang = new NSQuaTrinhCongTacDang();
        NSNhanXet nhanXet = new NSNhanXet();
        NSKiLuat kiLuat = new NSKiLuat();
        NSNgoaiNgu ngoaiNgu = new NSNgoaiNgu();
        NSKhenThuong khenThuong = new NSKhenThuong();
        NSHopDongLaoDong hopdong = new NSHopDongLaoDong();
        NSPhuCapChucVu phuCapChucVu = new NSPhuCapChucVu();
        NSPhuCapKhac phucCapKhac = new NSPhuCapKhac();
        NSNgachBacLuong ngach = new NSNgachBacLuong();
        BO.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
        BO.NSNhanVien nhanvien = new Viewer.BusinessObject.BusinessEntities.NSNhanVien();
        BO.NSToChuc tochuc = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataAllControl();
            }
        }

        /// <summary>
        /// Set enable cho các nút tạo mới khi view
        /// </summary>
        /// <param name="type"></param>
        private void SetEnableControl(bool type)
        {
            btnTaoMoi_HDLD.Disabled = type;
            btnTaoMoi_PCCV.Disabled = type;
            btnTaoMoi_PCK.Disabled = type;
            btnTaoMoi_NBL.Disabled = type;
            btnTaoMoi_NgoaiNgu.Disabled = type;
            btnTaoMoi_KT.Disabled = type;
            btnTaoMoi_KT.Disabled = type;
            btnTaoMoi_NX.Disabled = type;
            btnTaoMoi_QTCT.Disabled = type;
            btnTaoMoi_QuaTrinhCongTacDang.Disabled = type;
            btnTaoMoi_DT.Disabled = type;
            btnSave_QH.Disabled = type;
            btnSave_QHBT.Disabled = type;
            btnTaoMoi_NgheNghiep.Disabled = type;
            btnTaoMoi_NghiPhep.Disabled = type;
            btnTaoMoi_QTGDBaoHiem.Disabled = type;
            btnTaoMoi_TrangThai.Disabled = type;
            btnTaoMoi_TrinhDoChuyenMon.Disabled = type;
            btnTaoMoi_BangCap.Disabled = type;
            btnTaoMoi_LiLuan.Disabled = type;
            btnTaoMoi_QLNN.Disabled = type;
            btnTaoMoi_TDTH.Disabled = type;
        }

        private void LoadDatetime()
        {
            dtNgayCapCMND.Text = DateTime.Today.ToString("dd/MM/yyyy");
            dtNgaySinh.Text = DateTime.Today.ToString("dd/MM/yyyy");
        }

        #region LoadDataAllControl load data cho các combobox
        private void LoadDataAllControl()
        {
            storeTonGiao.DataSource = common.LoadDataCommonByCodeKey(common.TonGiao);
            storeTonGiao.DataBind();
            storeGioiTinh.DataSource = common.LoadDataCommonByCodeKey(common.GioiTinh);
            storeGioiTinh.DataBind();
            storeDanToc.DataSource = common.LoadDataCommonByCodeKey(common.DanToc);
            storeDanToc.DataBind();
            storeTrinhDoPhoThong.DataSource = common.LoadDataCommonByCodeKey(common.TrinhDoGiaoDucPhoThong);
            storeTrinhDoPhoThong.DataBind();
            storeHeGD.DataSource = common.LoadDataCommonByCodeKey(common.HinhThucDaoTao);
            storeHeGD.DataBind();
            storeNhomMau.DataSource = common.LoadDataCommonByCodeKey(common.NhomMau);
            storeNhomMau.DataBind();            
            storeQuanHam.DataSource = common.LoadDataCommonByCodeKey(common.QuanHam);
            storeQuanHam.DataBind();
            stroreDanhHieu.DataSource = common.LoadDataCommonByCodeKey(common.DanhHieu);
            stroreDanhHieu.DataBind();
            storeHangThuongBinh.DataSource = common.LoadDataCommonByCodeKey(common.XepHangDonVi);
            storeHangThuongBinh.DataBind();
            storeConGiaDinh.DataSource = common.LoadDataCommonByCodeKey(common.DoiTuongChinhSach);
            storeConGiaDinh.DataBind();
        }
        #endregion

        /// <summary>
        /// Khởi tạo lại các control khi tạo mới
        /// </summary>
        private void ResetControl()
        {
            txtTrinhDoChuyenMon.Text = string.Empty;
            txtCoQuanSuDung.Text = string.Empty;
            txtTenKhaiSinh.Text = string.Empty;
            txtSoBHXH.Text = string.Empty;
            txtTenGoiKhac.Text = string.Empty;
            cbbGioiTinh.SelectedItem.Value = string.Empty;
            dtNgaySinh.Text = string.Empty;
            txtNoiSinh.Text = string.Empty;
            txtQueQuan.Text = string.Empty;
            cbbDanToc.SelectedItem.Value = string.Empty;
            txtCMND.Text = string.Empty;
            dtNgayCapCMND.Text = string.Empty;
            dtNgayBatDauDongBaoHiem.Text = string.Empty;
            txtHoKhauThuongTru.Text = string.Empty;
            txtChoOHienNay.Text = string.Empty;
            cbbTonGiao.SelectedItem.Value = string.Empty;
            dtNgayTuyenDung.Text = string.Empty;
            txtCoQuanTuyenDung.Text = string.Empty;
            hdToChucId.Value = string.Empty;
            hdFlagToChuc.Value = string.Empty;
            cbbTrinhDoPhoThong.SelectedItem.Value = string.Empty;
            cbbHeGD.SelectedItem.Value = string.Empty;
            txtSoTruongCongTac.Text = string.Empty;
            txtNoiCapCMND.Text = string.Empty;
            txtChieuCao.Text = string.Empty;
            txtCanNang.Text = string.Empty;
            cbbNhomMau.SelectedItem.Value = string.Empty;
            dtNgayVaoDang.Text = string.Empty;
            dtNgayChinhThuc.Text = string.Empty;
            txtToChucChinhTri.Text = string.Empty;
            dtNgayNhapNgu.Text = string.Empty;
            dtNgayXuatNgu.Text = string.Empty;
            cbbQuanHam.SelectedItem.Value = string.Empty;
            cbbDanhHieu.SelectedItem.Value = string.Empty;
            cbbHangThuongBinh.SelectedItem.Value = string.Empty;
            cbbConGiaDinh.SelectedItem.Value = string.Empty;

            storeTrangThaiLamViec.RemoveAll();
            storeTrangThaiLamViec.DataBind();
            storeQTCT.RemoveAll();
            storeQTCT.DataBind();
            storeNgheNghiep.RemoveAll();
            storeNgheNghiep.DataBind();
            storeHopDongLaoDong.RemoveAll();
            storeHopDongLaoDong.DataBind();
            storeNgachCongChuc.RemoveAll();
            storeNgachCongChuc.DataBind();
            storePhuCapChucVu.RemoveAll();
            storePhuCapChucVu.DataBind();
            storePhuCapKhac.RemoveAll();
            storePhuCapKhac.DataBind();
            storeNgoaiNgu.RemoveAll();
            storeNgoaiNgu.DataBind();
            storeQuaTrinhCongTacDang.RemoveAll();
            storeQuaTrinhCongTacDang.DataBind();
            storeQuanHeBanThan.RemoveAll();
            storeQuanHeBanThan.DataBind();
            storeQuanHeBenVo.RemoveAll();
            storeQuanHeBenVo.DataBind();
            storeDaoTao.RemoveAll();
            storeDaoTao.DataBind();
            storeKhenThuong.RemoveAll();
            storeKhenThuong.DataBind();
            storeKiLuat.RemoveAll();
            storeKiLuat.DataBind();
            storeNhanXet.RemoveAll();
            storeNhanXet.DataBind();
            storeQTGDBaoHiem.RemoveAll();
            storeQTGDBaoHiem.DataBind();
            storeNghiPhep.RemoveAll();
            storeNghiPhep.DataBind();
            storeTrinhDoChuyenMon.RemoveAll();
            storeTrinhDoChuyenMon.DataBind();
            storeBangCap.RemoveAll();
            storeBangCap.DataBind();
            storeLiLuanChinhTri.RemoveAll();
            storeLiLuanChinhTri.DataBind();
            storeQuanLiNhaNuoc.RemoveAll();
            storeQuanLiNhaNuoc.DataBind();
            storeTrinhDoTinHoc.RemoveAll();
            storeTrinhDoTinHoc.DataBind();

            FileUpload.Text = string.Empty;
            Image.ImageUrl = string.Empty;
            txtLichSuChinhTri.Text = string.Empty;
            txtQuanHeTCNN.Text = string.Empty;
            txtQuanHeNhanThanNuocNgoai.Text = string.Empty;
            txtSoHieu.Text = string.Empty;
            txtCoQuanQuanLi.Text = string.Empty;
            hdCoQuanQuanLiId.Value = string.Empty;
            LoadDatetime();
        }

        /// <summary>
        /// Load data theo nhân viên id 
        /// </summary>
        /// <param name="nhanvienId"></param>

        private void LoadData(int nhanvienId)
        {
            NSNhanVien item = nhanvien.LoadNhanVienById(nhanvienId);
            txtSoHieu.Text = item.SoHieu;
            txtTenKhaiSinh.Text = item.HoTenKhaiSinh;
            txtSoBHXH.Text = item.SoBHXH;
            txtTenGoiKhac.Text = item.HoTenKhac;
            cbbGioiTinh.SelectedItem.Value = item.GioiTinhID > 0 ? item.GioiTinhID.ToString() : string.Empty;
            dtNgaySinh.Text = item.NgaySinh.ToString("dd/MM/yyyy");
            txtNoiSinh.Text = item.DiaChiNoiSinh;
            txtQueQuan.Text = tochuc.LayDiaChi(item.DiachiIDQueQuan);
            cbbDanToc.SelectedItem.Value = item.DanTocID > 0 ? item.DanTocID.ToString() : string.Empty;
            txtCMND.Text = !string.IsNullOrEmpty(item.SoCMND) ? item.SoCMND.ToString() : string.Empty;
            dtNgayCapCMND.Text = item.NgayCap.HasValue ? ((DateTime)item.NgayCap).ToString("dd/MM/yyyy") : string.Empty;
            txtHoKhauThuongTru.Text = tochuc.LayDiaChi((int)item.DiaChiIDHoKhau);
            txtChoOHienNay.Text = tochuc.LayDiaChi((int)item.DiaChiIDNoiOHienTai);

            dtNgayTuyenDung.Text = item.NgayTuyenDung.HasValue ? item.NgayTuyenDung.Value.ToString("dd/MM/yyyy") : string.Empty;
            hdFlagToChuc.Value = item.ThuocSoYTeFlag;
            if (item.ThuocSoYTeFlag.HasValue && item.ThuocSoYTeFlag.Value)
            {
                txtCoQuanTuyenDung.Text = item.ToChucIDTuyenDung > 0 ? tochuc.LayTenToChuc((int)item.ToChucIDTuyenDung) : item.CoQuanKhac;
                hdToChucId.Value = item.ToChucIDTuyenDung.ToString();
            }
            else
            {
                txtCoQuanTuyenDung.Text = item.CoQuanKhac;
            }

            cbbTonGiao.SelectedItem.Value = item.TonGiaoID.ToString();
            txtCoQuanQuanLi.Text = item.CoQuanQuanLiId.Value > 0 ? tochuc.LayTenToChuc(item.CoQuanQuanLiId.Value) : string.Empty;
            hdCoQuanQuanLiId.Value = item.CoQuanQuanLiId.Value.ToString();
            txtCoQuanSuDung.Text = item.CoQuanSuDungId > 0 ? tochuc.LayTenToChuc(item.CoQuanSuDungId.Value) : string.Empty;
            hdCQSDId.Text = item.CoQuanSuDungId.Value.ToString();
            cbbTrinhDoPhoThong.SelectedItem.Value = item.TrinhDoGiaoDucPhoThongID.Value > 0 ? item.TrinhDoGiaoDucPhoThongID.Value.ToString() : string.Empty;
            cbbHeGD.SelectedItem.Value = item.HeGiaoDucPhoThongID.Value > 0 ? item.HeGiaoDucPhoThongID.Value.ToString() : string.Empty;
            dtNgayBatDauDongBaoHiem.Text = item.NgayBatDauDongBaoHiem.HasValue ? item.NgayBatDauDongBaoHiem.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtTrinhDoChuyenMon.Text = item.ThongTinTrinhDoChuyenMon.ToString();
            txtSoTruongCongTac.Text = item.SoTruongCongTac;
            txtNoiCapCMND.Text = item.NoiCap;
            txtChieuCao.Text = item.ChieuCao.ToString();
            txtCanNang.Text = item.CanNang.Value.ToString();
            cbbNhomMau.SelectedItem.Value = item.NhomMauID.Value > 0 ? item.NhomMauID.Value.ToString() : string.Empty;
            dtNgayVaoDang.Text = item.NgayVaoDangCSVN.HasValue ? item.NgayVaoDangCSVN.Value.ToString("dd/MM/yyyy") : string.Empty;
            dtNgayChinhThuc.Text = item.NgayChinhThucVaoDangCSVN.HasValue ? item.NgayChinhThucVaoDangCSVN.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtToChucChinhTri.Text = item.ToChucChinhTriThamGia;
            dtNgayNhapNgu.Text = item.NgayNhapNgu.HasValue ? item.NgayNhapNgu.Value.ToString("dd/MM/yyyy") : string.Empty;
            dtNgayXuatNgu.Text = item.NgayXuatNgu.HasValue ? item.NgayXuatNgu.Value.ToString("dd/MM/yyyy") : string.Empty;
            cbbQuanHam.SelectedItem.Value = item.QuanHamCaoNhatID.Value > 0 ? item.QuanHamCaoNhatID.Value.ToString() : string.Empty;
            cbbDanhHieu.SelectedItem.Value = item.DanhHieuCaoNhatID.Value > 0 ? item.DanhHieuCaoNhatID.Value.ToString() : string.Empty;
            cbbHangThuongBinh.SelectedItem.Value = item.HangThuongBinhID.Value > 0 ? item.HangThuongBinhID.Value.ToString() : string.Empty;
            cbbConGiaDinh.SelectedItem.Value = item.ConGiaDinhChinhSachID.Value > 0 ? item.ConGiaDinhChinhSachID.Value.ToString() : string.Empty;
            txtLichSuChinhTri.Text = item.LichSuChinhTri;
            txtQuanHeNhanThanNuocNgoai.Text = item.QuanHeNhanThanNuocNgoai;
            txtQuanHeTCNN.Text = item.QuanHeToChucNuocNgoai;

            if (!string.IsNullOrEmpty(item.Image))
            {
                Image.ImageUrl = "~/Images/" + item.Image;

            }
            LoadDataUC(nhanvienId, "HDLD");
            LoadDataUC(nhanvienId, "PCCV");
            LoadDataUC(nhanvienId, "PCK");
            LoadDataUC(nhanvienId, "NBL");
            LoadDataUC(nhanvienId, "NgoaiNgu");
            LoadDataUC(nhanvienId, "KT");
            LoadDataUC(nhanvienId, "KL");
            LoadDataUC(nhanvienId, "NX");
            LoadDataUC(nhanvienId, "QTCT");
            LoadDataUC(nhanvienId, "QuaTrinhCongTacDang");
            LoadDataUC(nhanvienId, "DT");
            LoadDataUC(nhanvienId, "QH");
            LoadDataUC(nhanvienId, "QHBT");
            LoadDataUC(nhanvienId, "NgheNghiep");
            LoadDataUC(nhanvienId, "ChucVu");
            LoadDataUC(nhanvienId, "NghiPhep");
            LoadDataUC(nhanvienId, "QTGD");
            LoadDataUC(nhanvienId, "TrangThaiLamViec");
            LoadDataUC(nhanvienId, "TDCM");
            LoadDataUC(nhanvienId, "BangCap");
            LoadDataUC(nhanvienId, "LiLuan");
            LoadDataUC(nhanvienId, "QLNN");
            LoadDataUC(nhanvienId, "TDTH");

            hdHoKhauThuongTruId.Text = item.DiaChiIDHoKhau.ToString();
            hdQueQuanId.Text = item.DiachiIDQueQuan.ToString();
            hdChoOHienNayId.Text = item.DiaChiIDNoiOHienTai.ToString();

        }

        protected void btnLookUp_Click(object sender, DirectEventArgs e)
        {
            UCLookUpDiaChi.ShowDialogDiaChi();
            X.Mask.Hide();
        }
      
          /// <summary>
        /// Lưu nhân viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                //baoDT

                #region Set Property

                int gioiTinhId = !string.IsNullOrEmpty(cbbGioiTinh.SelectedItem.Value) ? Convert.ToInt16(cbbGioiTinh.SelectedItem.Value) : 0;
                string noiSinh = !string.IsNullOrEmpty(txtNoiSinh.Text) ? txtNoiSinh.Text : string.Empty;
                int quequanId = !string.IsNullOrEmpty(hdQueQuanId.Text) ? Convert.ToInt16(hdQueQuanId.Value) : 0;
                int dantocId = !string.IsNullOrEmpty(cbbDanToc.SelectedItem.Value) ? Convert.ToInt16(cbbDanToc.SelectedItem.Value) : 0;
                int noiDangKyHoKhauId = !string.IsNullOrEmpty(hdHoKhauThuongTruId.Text) ? Convert.ToInt16(hdHoKhauThuongTruId.Text) : 0;
                int trangThaiLamViec = 0;
                int coquanTD = 0;
                bool tochucFlag = !string.IsNullOrEmpty(hdFlagToChuc.Value.ToString()) ? Convert.ToBoolean(hdFlagToChuc.Value) : false;
                if (tochucFlag)
                {
                    coquanTD = !string.IsNullOrEmpty(hdToChucId.Value.ToString()) ? Convert.ToInt16(hdToChucId.Value) : 0;
                }
                int noiOHienNayId = !string.IsNullOrEmpty(hdChoOHienNayId.Text) ? Convert.ToInt16(hdChoOHienNayId.Text) : 0;
                int trinhdoPhoThong = !string.IsNullOrEmpty(cbbTrinhDoPhoThong.SelectedItem.Value) ? Convert.ToInt16(cbbTrinhDoPhoThong.SelectedItem.Value) : 0;
                int nhomMau = !string.IsNullOrEmpty(cbbNhomMau.SelectedItem.Value) ? Convert.ToInt16(cbbNhomMau.SelectedItem.Value) : 0;
                int heGD = !string.IsNullOrEmpty(cbbHeGD.SelectedItem.Value) ? Convert.ToInt16(cbbHeGD.SelectedItem.Value) : 0;
                int quanHam = !string.IsNullOrEmpty(cbbQuanHam.SelectedItem.Value) ? Convert.ToInt16(cbbQuanHam.SelectedItem.Value) : 0;
                int danhHieu = !string.IsNullOrEmpty(cbbDanhHieu.SelectedItem.Value) ? Convert.ToInt16(cbbDanhHieu.SelectedItem.Value) : 0;
                int hangThuongBinh = !string.IsNullOrEmpty(cbbHangThuongBinh.SelectedItem.Value) ? Convert.ToInt16(cbbHangThuongBinh.SelectedItem.Value) : 0;
                int giaDinhChinhsach = !string.IsNullOrEmpty(cbbConGiaDinh.SelectedItem.Value) ? Convert.ToInt16(cbbConGiaDinh.SelectedItem.Value) : 0;
                int coquanQL = !string.IsNullOrEmpty(hdCoQuanQuanLiId.Value.ToString()) ? Convert.ToInt16(hdCoQuanQuanLiId.Value.ToString()) : 0;
                int coquanSuDung = !string.IsNullOrEmpty(hdCQSDId.Text) ? Convert.ToInt16(hdCQSDId.Text) : 0;
                #endregion

                int canNang = !string.IsNullOrEmpty(txtCanNang.Text) ? Convert.ToInt16(txtCanNang.Text) : 0;
                int chieuCao = !string.IsNullOrEmpty(txtChieuCao.Text) ? Convert.ToInt16(txtChieuCao.Text) : 0;
                DateTime ngayVaoDang = dtNgayVaoDang.SelectedDate;
                DateTime ngayNhapNgu = dtNgayNhapNgu.SelectedDate;
                DateTime ngayBatDauDongBaoHiem = dtNgayBatDauDongBaoHiem.SelectedDate;
                DateTime ngayXuatNgu = dtNgayXuatNgu.SelectedDate;
                int tongiaoId = !string.IsNullOrEmpty(cbbTonGiao.SelectedItem.Value) ? Convert.ToInt16(cbbTonGiao.SelectedItem.Value) : 0;

                //Convert session to Object
                hdldObject = (List<NSHopDongLaoDong>)Session["HDLD"];
                pccvObject = (List<NSPhuCapChucVu>)Session["PCCV"];
                pckObject = (List<NSPhuCapKhac>)Session["PCK"];
                nblObject = (List<NSNgachBacLuong>)Session["NBL"];
                ngoaiNguObject = (List<NSNgoaiNgu>)Session["NgoaiNgu"];
                khenThuongObject = (List<NSKhenThuong>)Session["KT"];
                kiLuatObject = (List<NSKiLuat>)Session["KL"];
                nhanXetObject = (List<NSNhanXet>)Session["NX"];
                qtctObject = (List<NSQuaTrinhCongTac>)Session["QTCT"] != null ? (List<NSQuaTrinhCongTac>)Session["QTCT"] : new List<NSQuaTrinhCongTac>();
                qtctDangObject = (List<NSQuaTrinhCongTacDang>)Session["QuaTrinhCongTacDang"] != null ? (List<NSQuaTrinhCongTacDang>)Session["QuaTrinhCongTacDang"] : new List<NSQuaTrinhCongTacDang>();
                dtObject = (List<NSDaoTao>)Session["DT"];
                quanHeBanThanObject = (List<NSQuanHeGiaDinh>)Session["QHBT"];
                quanHeObject = (List<NSQuanHeGiaDinh>)Session["QH"];
                ngheNghiepObject = (List<NSNgheNghiep>)Session["NgheNghiep"];
                nghiPhepObject = (List<NSNghiPhep>)Session["NghiPhep"];
                qtBaoHiemObject = (List<NSQuaTrinhGianDoanBaoHiem>)Session["QTGD"];
                trangThaiObject = (List<NSTrangThaiLamViec>)Session["TrangThaiLamViec"];
                trinhDoChuyenMonObject = (List<NSTrinhDoChuyenMon>)Session["TDCM"];
                bangCapObject = (List<NSBangCap>)Session["BangCap"];
                liLuanObject = (List<NSLiLuanChinhTri>)Session["LiLuan"];
                quanLiNNObject = (List<NSQuanLiNhaNuoc>)Session["QLNN"];
                trinhDoTinHocObject = (List<NSTrinhDoTinHoc>)Session["TDTH"];

                if (hdStatusUC.Text.Equals("AddNew"))
                {
                    nhanvien.InsertNhanVien(0, "AddNew", txtTenKhaiSinh.Text, txtTenGoiKhac.Text, gioiTinhId, Convert.ToDateTime(dtNgaySinh.Text), noiSinh, quequanId, dantocId, txtCMND.Text, Convert.ToDateTime(dtNgayCapCMND.Text)
                  , noiDangKyHoKhauId, noiOHienNayId, Convert.ToDateTime(dtNgayTuyenDung.Text), coquanTD, trinhdoPhoThong, heGD, txtTrinhDoChuyenMon.Text, txtSoTruongCongTac.Text, txtTinhTrangSucKhoe.Text, chieuCao, canNang, nhomMau, ngayVaoDang,
                  dtNgayChinhThuc.SelectedDate, txtToChucChinhTri.Text, ngayNhapNgu, ngayXuatNgu, quanHam, danhHieu, hangThuongBinh, giaDinhChinhsach, 0, txtNoiCapCMND.Text, tongiaoId, hdAnh.Text, titleAnh.Text, hdldObject, pccvObject, pckObject, nblObject, ngoaiNguObject
                  , khenThuongObject, kiLuatObject, nhanXetObject, qtctObject, qtctDangObject, dtObject, txtQuanHeNhanThanNuocNgoai.Text, txtQuanHeTCNN.Text, txtLichSuChinhTri.Text, txtSoHieu.Text, coquanQL, coquanSuDung,
                  quanHeBanThanObject, quanHeObject, txtSoBHXH.Text, ngheNghiepObject, trangThaiLamViec, nghiPhepObject, qtBaoHiemObject,
                  ngayBatDauDongBaoHiem, tochucFlag, txtCoQuanTuyenDung.Text, trangThaiObject, trinhDoChuyenMonObject, bangCapObject, liLuanObject, quanLiNNObject,trinhDoTinHocObject);
                }
                else
                {
                    nhanvien.InsertNhanVien(Convert.ToInt32(hdNhanVienId.Text), "Update", txtTenKhaiSinh.Text, txtTenGoiKhac.Text, gioiTinhId, Convert.ToDateTime(dtNgaySinh.Text), noiSinh, quequanId, dantocId, txtCMND.Text, Convert.ToDateTime(dtNgayCapCMND.Text)
               , noiDangKyHoKhauId, noiOHienNayId, Convert.ToDateTime(dtNgayTuyenDung.Text), coquanTD,
               trinhdoPhoThong, heGD, txtTrinhDoChuyenMon.Text, txtSoTruongCongTac.Text, txtTinhTrangSucKhoe.Text, chieuCao, canNang, nhomMau, ngayVaoDang,
               dtNgayChinhThuc.SelectedDate, txtToChucChinhTri.Text, ngayNhapNgu, ngayXuatNgu, quanHam, danhHieu, hangThuongBinh, giaDinhChinhsach, 0, txtNoiCapCMND.Text, tongiaoId, hdAnh.Text,
               titleAnh.Text, hdldObject, pccvObject, pckObject, nblObject, ngoaiNguObject, khenThuongObject, kiLuatObject
               , nhanXetObject, qtctObject, qtctDangObject, dtObject, txtQuanHeNhanThanNuocNgoai.Text, txtQuanHeTCNN.Text, txtLichSuChinhTri.Text,
               txtSoHieu.Text, coquanQL, coquanSuDung, quanHeBanThanObject, quanHeObject, txtSoBHXH.Text, ngheNghiepObject, trangThaiLamViec,
               nghiPhepObject, qtBaoHiemObject, ngayBatDauDongBaoHiem, tochucFlag, txtCoQuanTuyenDung.Text, trangThaiObject, trinhDoChuyenMonObject, bangCapObject, liLuanObject, quanLiNNObject, trinhDoTinHocObject);
                }

                wdUCNhanVien.Hide();
                ((NhanSu)this.Page).LoadGrid(hdNodeId.Text, hdNodeName.Text);
                X.Mask.Hide();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Load các grid bên trong nhân viên
        /// </summary>
        /// <param name="nhanvienId"></param>
        /// <param name="type"></param>
        public void LoadDataUC(int nhanvienId, string type)
        {
            if (type.Equals("HDLD"))
            {
                storeHopDongLaoDong.DataSource = hopdong.LoadHDLDByNhanVienId(nhanvienId);
                storeHopDongLaoDong.DataBind();
                Session[type] = hopdong.LoadHDLDByNhanVienId(nhanvienId);
            }
            else if (type.Equals("PCCV"))
            {
                storePhuCapChucVu.DataSource = phuCapChucVu.LoadPCCVByNhanVienId(nhanvienId);
                storePhuCapChucVu.DataBind();
                Session[type] = phuCapChucVu.LoadPCCVByNhanVienId(nhanvienId);

            }
            else if (type.Equals("PCK"))
            {
                storePhuCapKhac.DataSource = phucCapKhac.LoadPCKByNhanVienId(nhanvienId);
                storePhuCapKhac.DataBind();
                Session[type] = phucCapKhac.LoadPCKByNhanVienId(nhanvienId);
            }
            else if (type.Equals("QTGD"))
            {
                storeQTGDBaoHiem.DataSource = baoHiem.LoadQTGDByNhanVienId(nhanvienId);
                storeQTGDBaoHiem.DataBind();
                Session[type] = baoHiem.LoadQTGDByNhanVienId(nhanvienId);
            }
            else if (type.Equals("NBL"))
            {
                storeNgachCongChuc.DataSource = ngach.LoadNBLByNhanVienId(nhanvienId);
                storeNgachCongChuc.DataBind();
                Session[type] = ngach.LoadNBLByNhanVienId(nhanvienId);
            }
            else if (type.Equals("TrangThaiLamViec"))
            {
                storeTrangThaiLamViec.DataSource = trangThai.LoadTrangThaiLamViecNhanVienId(nhanvienId);
                storeTrangThaiLamViec.DataBind();
                Session[type] = trangThai.LoadTrangThaiLamViecNhanVienId(nhanvienId);
            }
            else if (type.Equals("NgoaiNgu"))
            {
                storeNgoaiNgu.DataSource = ngoaiNgu.LoadNgoaiNguNhanVienId(nhanvienId);
                storeNgoaiNgu.DataBind();
                Session[type] = ngoaiNgu.LoadNgoaiNguNhanVienId(nhanvienId);
            }
            else if (type.Equals("KT"))
            {
                storeKhenThuong.DataSource = khenThuong.LoadKTByNhanVienId(nhanvienId);
                storeKhenThuong.DataBind();
                Session[type] = khenThuong.LoadKTByNhanVienId(nhanvienId);
            }
            else if (type.Equals("KL"))
            {
                storeKiLuat.DataSource = kiLuat.LoadKLByNhanVienId(nhanvienId);
                storeKiLuat.DataBind();
                Session[type] = kiLuat.LoadKLByNhanVienId(nhanvienId);
            }
            else if (type.Equals("NX"))
            {
                storeNhanXet.DataSource = nhanXet.LoadNXByNhanVienId(nhanvienId);
                storeNhanXet.DataBind();
                Session[type] = nhanXet.LoadNXByNhanVienId(nhanvienId);
            }
            else if (type.Equals("QTCT"))
            {
                storeQTCT.DataSource = qtct.LoadQTCTByNhanVienId(nhanvienId);
                storeQTCT.DataBind();
                Session[type] = qtct.LoadQTCTByNhanVienId(nhanvienId);
            }
            else if (type.Equals("QuaTrinhCongTacDang"))
            {
                storeQuaTrinhCongTacDang.DataSource = quaTrinhCongTacDang.LoadQTCTDangByNhanVienId(nhanvienId);
                storeQuaTrinhCongTacDang.DataBind();
                Session[type] = quaTrinhCongTacDang.LoadQTCTDangByNhanVienId(nhanvienId);
            }
            else if (type.Equals("DT"))
            {
                storeDaoTao.DataSource = dt.LoadDTByNhanVienId(nhanvienId);
                storeDaoTao.DataBind();
                Session[type] = dt.LoadDTByNhanVienId(nhanvienId);
            }
            else if (type.Equals("QH"))
            {
                storeQuanHeBenVo.DataSource = quanHe.LoadQHByNhanVienId(nhanvienId, type);
                storeQuanHeBenVo.DataBind();
                Session[type] = quanHe.LoadQHByNhanVienId(nhanvienId, type);
            }
            else if (type.Equals("QHBT"))
            {
                storeQuanHeBanThan.DataSource = quanHe.LoadQHByNhanVienId(nhanvienId, type);
                storeQuanHeBanThan.DataBind();
                Session[type] = quanHe.LoadQHByNhanVienId(nhanvienId, type);
            }
            else if (type.Equals("NgheNghiep"))
            {
                storeNgheNghiep.DataSource = ngheNghiep.LoadNgheNghiepByNhanVienId(nhanvienId);
                storeNgheNghiep.DataBind();
                Session[type] = ngheNghiep.LoadNgheNghiepByNhanVienId(nhanvienId);
            }
            else if (type.Equals("NghiPhep"))
            {
                storeNghiPhep.DataSource = nghiPhep.LoadNghiPhepByNhanVienId(nhanvienId);
                storeNghiPhep.DataBind();
                Session[type] = nghiPhep.LoadNghiPhepByNhanVienId(nhanvienId);
            }
            else if (type.Equals("TDCM"))
            {
                storeTrinhDoChuyenMon.DataSource = trinhDoChuyenMon.LoadTDCMByNhanVienId(nhanvienId);
                storeTrinhDoChuyenMon.DataBind();
                Session[type] = trinhDoChuyenMon.LoadTDCMByNhanVienId(nhanvienId);
            }
            else if (type.Equals("BangCap"))
            {
                storeBangCap.DataSource = bangCap.LoadData(nhanvienId);
                storeBangCap.DataBind();
                Session[type] = bangCap.LoadData(nhanvienId);
            }
            else if (type.Equals("LiLuan"))
            {
                storeLiLuanChinhTri.DataSource = liLuan.LoadData(nhanvienId);
                storeLiLuanChinhTri.DataBind();
                Session[type] = liLuan.LoadData(nhanvienId);
            }
            else if (type.Equals("QLNN"))
            {
                storeQuanLiNhaNuoc.DataSource = quanLiNN.LoadData(nhanvienId);
                storeQuanLiNhaNuoc.DataBind();
                Session[type] = quanLiNN.LoadData(nhanvienId);
            }
            else if (type.Equals("TDTH"))
            {
                storeTrinhDoTinHoc.DataSource = trinhDoTinHoc.LoadData(nhanvienId);
                storeTrinhDoTinHoc.DataBind();
                Session[type] = trinhDoTinHoc.LoadData(nhanvienId);
            }
        }

        public void ShowDialog(string type, int nhanvienId, string nodeId, string nodeName)
        {
            Image.ImageUrl = string.Empty;
            FileUpload.Clear();

            #region Reset session list
            Session["TrangThaiLamViec"] = null;
            Session["NghiPhep"] = null;
            Session["PCCV"] = null;
            Session["PCK"] = null;
            Session["HDLD"] = null;
            Session["NBL"] = null;
            Session["NgoaiNgu"] = null;
            Session["KT"] = null;
            Session["KL"] = null;
            Session["NX"] = null;
            Session["QTCT"] = null;
            Session["QuaTrinhCongTacDang"] = null;
            Session["DT"] = null;
            Session["QHBT"] = null;
            Session["QH"] = null;
            Session["NgheNghiep"] = null;
            Session["TDCM"] = null;
            Session["ChucVu"] = null;
            Session["QTGD"] = null;
            Session["BangCap"] = null;
            Session["LiLuan"] = null;
            Session["QLNN"] = null;
            Session["TDTH"] = null;
            #endregion

            typeNhanVien.Value = type;
            TabPanel1.SetActiveTab(Panel1);
            hdNodeId.Text = nodeId;
            hdNodeName.Text = nodeName;

            if (type.Equals("Edit"))
            {
                hdNhanVienId.Text = nhanvienId.ToString();
                wdUCNhanVien.Title = "Sửa thông tin nhân viên";
                LoadData(nhanvienId);
                hdStatusUC.Text = "Edit";
                btnSave.Hidden = false;
                SetEnableControl(false);
            }
            else if (type.Equals("View"))
            {
                hdNhanVienId.Text = nhanvienId.ToString();
                wdUCNhanVien.Title = "Xem thông tin nhân viên";
                LoadData(nhanvienId);
                btnSave.Hidden = true;
                hdStatusUC.Text = "View";
                SetEnableControl(true);

            }
            else if (type.Equals("AddNew"))
            {

                ResetControl();
                LoadDataAllControl();
                wdUCNhanVien.Title = "Tạo mới nhân viên";
                hdStatusUC.Text = "AddNew";
                btnSave.Hidden = false;
                SetEnableControl(false);
                txtCoQuanQuanLi.Text = nodeName;
                hdCoQuanQuanLiId.Value = nodeId;

            }
            wdUCNhanVien.Show();
        }

        #region xử lí chọn ảnh
        protected void btnSaveImage_Click(object sender, DirectEventArgs e)
        {
            string path = Server.MapPath("~/Images");
            FileUpload.PostedFile.SaveAs(path);

        }
        [DirectMethod]
        public void SaveImage(string title)
        {
            string strFileType = System.IO.Path.GetExtension(FileUpload.FileName).ToString().ToLower();
            if (strFileType.Equals(".jpg") || strFileType.Equals(".gif") || strFileType.Equals(".bmp") || strFileType.Equals(".jpeg") || strFileType.Equals(".png"))
            {
                string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
                FileStream writeStream = new FileStream(Server.MapPath("~/Images/" + strFileName + strFileType), FileMode.Create, FileAccess.Write);
                ReadWriteStream(FileUpload.FileContent, writeStream);
                Image.ImageUrl = "~/Images/" + strFileName + strFileType;
                hdAnh.Text = strFileName + strFileType;
                titleAnh.Text = title;
            }
            else
            {
                X.Msg.Alert("Thông báo", "Xin vui lòng chọn ảnh.").Show();
            }

        }
        private void ReadWriteStream(Stream readStream, Stream writeStream)
        {
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();
        }
        #endregion

        /// <summary>
        /// Load các usercontrol trong nhân viên
        /// </summary>
        /// <param name="type"></param>
        /// <param name="HDLDId"></param>
        /// <param name="typeUC"></param>
        [DirectMethod]
        public void ShowDialogUC(string type, string HDLDId, string typeUC)
        {
            if (type.Equals("Delete"))
            {
                int id = Convert.ToInt16(HDLDId);
                if (typeUC.Equals("HDLD"))
                {
                    hopdong.DeleteHDLD(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("PCCV"))
                {
                    phuCapChucVu.DeletePCCV(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("PCK"))
                {
                    phucCapKhac.DeletePCK(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("NBL"))
                {
                    ngach.DeleteNBL(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("QTGD"))
                {
                    baoHiem.DeleteQTGD(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("NgoaiNgu"))
                {
                    ngoaiNgu.DeleteNgoaiNgu(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("KT"))
                {
                    khenThuong.DeleteKT(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("KL"))
                {
                    kiLuat.DeleteKL(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("TrangThaiLamViec"))
                {
                    trangThai.DeleteTrangThaiLamViec(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("NX"))
                {
                    nhanXet.DeleteNX(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("QTCT"))
                {
                    qtct.DeleteQTCT(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("QuaTrinhCongTacDang"))
                {
                    quaTrinhCongTacDang.DeleteQTCTDang(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("DT"))
                {
                    dt.DeleteDT(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("QHBT"))
                {
                    quanHe.DeleteQH(id, typeUC);
                    ReloadGridQuanHe(typeUC);
                }
                else if (typeUC.Equals("QH"))
                {
                    quanHe.DeleteQH(id, typeUC);
                    ReloadGridQuanHe(typeUC);
                }
                else if (typeUC.Equals("NgheNghiep"))
                {
                    ngheNghiep.DeleteNN(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("NghiPhep"))
                {
                    nghiPhep.DeleteNghiPhep(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("TDCM"))
                {
                    trinhDoChuyenMon.DeleteTDCM(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("BangCap"))
                {
                    bangCap.Delete(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("LiLuan"))
                {
                    liLuan.Delete(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("QLNN"))
                {
                    quanLiNN.Delete(id);
                    ReloadGrid(typeUC);
                }
                else if (typeUC.Equals("TDTH"))
                {
                    trinhDoTinHoc.Delete(id);
                    ReloadGrid(typeUC);
                }
            }
            else
            {
                //Trường hợp edit 1 nhân viên
                if (!string.IsNullOrEmpty(hdNhanVienId.Text))
                {
                    if (typeUC.Equals("HDLD"))
                    {
                        UCHopDongLaoDong.ShowDialog_HDLD(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("NgheNghiep"))
                    {
                        UCNgheNghiep.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("PCCV"))
                    {
                        UCPhuCapChucVu.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("PCK"))
                    {
                        UCPhuCapKhac.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("NBL"))
                    {
                        UCNgachBacLuong.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("NgoaiNgu"))
                    {
                        UCNgoaiNgu.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("QTGD"))
                    {
                        UCQuaTrinhGianDoanBaoHiem.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("KT"))
                    {
                        UCKhenThuong.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("KL"))
                    {
                        UCKiLuat.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("NX"))
                    {
                        UCNhanXet.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("QTCT"))
                    {
                        UCQuaTrinhCT.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm", txtCoQuanQuanLi.Text);
                    }
                    else if (typeUC.Equals("QuaTrinhCongTacDang"))
                    {
                        UCQuaTrinhCongTacDang.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("DT"))
                    {
                        UCDaoTao.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("QHBT"))
                    {
                        UCQuanHe.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm", "QHBT");
                    }
                    else if (typeUC.Equals("QH"))
                    {
                        UCQuanHe.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm", "QH");
                    }
                    else if (typeUC.Equals("NghiPhep"))
                    {
                        UCNghiPhep.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("TrangThaiLamViec"))
                    {
                        UCTrangThaiLamViec.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("TDCM"))
                    {
                        UCTrinhDoChuyenMon.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("BangCap"))
                    {
                        UCBangCap.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("LiLuan"))
                    {
                        UCLiLuanChinhTri.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("QLNN"))
                    {
                        UCQuanLiNhaNuoc.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                    else if (typeUC.Equals("TDTH"))
                    {
                        UCTrinhDoTinHoc.ShowDialog(type, HDLDId, hdNhanVienId.Text, "EditForm");
                    }
                }
                //Trường hợp tạo mới 1 nhân viên 
                else
                {
                    if (typeUC.Equals("HDLD"))
                    {
                        UCHopDongLaoDong.ShowDialog_HDLD(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("NgheNghiep"))
                    {
                        UCNgheNghiep.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("PCCV"))
                    {
                        UCPhuCapChucVu.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("PCK"))
                    {
                        UCPhuCapKhac.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("NBL"))
                    {
                        UCNgachBacLuong.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("NgoaiNgu"))
                    {
                        UCNgoaiNgu.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("KT"))
                    {
                        UCKhenThuong.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("KL"))
                    {
                        UCKiLuat.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("NghiPhep"))
                    {
                        UCNghiPhep.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("QTGD"))
                    {
                        UCQuaTrinhGianDoanBaoHiem.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("NX"))
                    {
                        UCNhanXet.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("QTCT"))
                    {
                        UCQuaTrinhCT.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm", txtCoQuanTuyenDung.Text);
                    }
                    else if (typeUC.Equals("QuaTrinhCongTacDang"))
                    {
                        UCQuaTrinhCongTacDang.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("DT"))
                    {
                        UCDaoTao.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("TrangThaiLamViec"))
                    {
                        UCTrangThaiLamViec.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("QHBT"))
                    {
                        UCQuanHe.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm", "QHBT");
                    }
                    else if (typeUC.Equals("QH"))
                    {
                        UCQuanHe.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm", "QH");
                    }
                    else if (typeUC.Equals("TDCM"))
                    {
                        UCTrinhDoChuyenMon.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("BangCap"))
                    {
                        UCBangCap.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("LiLuan"))
                    {
                        UCLiLuanChinhTri.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("QLNN"))
                    {
                        UCQuanLiNhaNuoc.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                    else if (typeUC.Equals("TDTH"))
                    {
                        UCTrinhDoTinHoc.ShowDialog(type, HDLDId, hdNhanVienId.Text, "NewForm");
                    }
                }
            }
            X.Mask.Hide();
        }
        /// <summary>
        /// Lưu item vào session tương ứng
        /// </summary>
        /// <param name="hdld"></param>
        #region
        public void GetHDLD(NSHopDongLaoDong hdld)
        {

            List<NSHopDongLaoDong> lst = (List<NSHopDongLaoDong>)Session["HDLD"];
            if (lst == null)
            {
                lst = new List<NSHopDongLaoDong>();
            }
            lst.Add(hdld);
            Session["HDLD"] = lst;
            storeHopDongLaoDong.DataSource = lst;
            storeHopDongLaoDong.DataBind();
        }
        public void GetPhuCapChucVu(NSPhuCapChucVu phucap)
        {
            List<NSPhuCapChucVu> lst = (List<NSPhuCapChucVu>)Session["PCCV"];
            if (lst == null)
            {
                lst = new List<NSPhuCapChucVu>();
            }
            lst.Add(phucap);
            Session["PCCV"] = lst;
            storePhuCapChucVu.DataSource = lst;
            storePhuCapChucVu.DataBind();

        }
        public void GetNghiPhep(NSNghiPhep nghiPhep)
        {
            List<NSNghiPhep> lst = (List<NSNghiPhep>)Session["NghiPhep"];
            if (lst == null)
            {
                lst = new List<NSNghiPhep>();
            }
            lst.Add(nghiPhep);
            Session["NghiPhep"] = lst;
            storeNghiPhep.DataSource = lst;
            storeNghiPhep.DataBind();
        }
        public void GetQTGD(NSQuaTrinhGianDoanBaoHiem qtgd)
        {
            List<NSQuaTrinhGianDoanBaoHiem> lst = (List<NSQuaTrinhGianDoanBaoHiem>)Session["QTGD"];
            if (lst == null)
            {
                lst = new List<NSQuaTrinhGianDoanBaoHiem>();
            }
            lst.Add(qtgd);
            Session["QTGD"] = lst;
            storeQTGDBaoHiem.DataSource = lst;
            storeQTGDBaoHiem.DataBind();
        }
        public void GetPhuCapKhac(NSPhuCapKhac phucap)
        {
            List<NSPhuCapKhac> lst = (List<NSPhuCapKhac>)Session["PCK"];
            if (lst == null)
            {
                lst = new List<NSPhuCapKhac>();
            }
            lst.Add(phucap);
            Session["PCK"] = lst;
            storePhuCapKhac.DataSource = lst;
            storePhuCapKhac.DataBind();

        }
        public void GetTrinhDoTinHoc(NSTrinhDoTinHoc trinhDo)
        {
            List<NSTrinhDoTinHoc> lst = (List<NSTrinhDoTinHoc>)Session["TDTH"];
            if (lst == null)
            {
                lst = new List<NSTrinhDoTinHoc>();
            }
            lst.Add(trinhDo);
            Session["TDTH"] = lst;
            storeTrinhDoTinHoc.DataSource = lst;
            storeTrinhDoTinHoc.DataBind();

        }
        public void GetQLNN(NSQuanLiNhaNuoc phucap)
        {
            List<NSQuanLiNhaNuoc> lst = (List<NSQuanLiNhaNuoc>)Session["QLNN"];
            if (lst == null)
            {
                lst = new List<NSQuanLiNhaNuoc>();
            }
            lst.Add(phucap);
            Session["QLNN"] = lst;
            storeQuanLiNhaNuoc.DataSource = lst;
            storeQuanLiNhaNuoc.DataBind();

        }
        public void GetNgachBacLuong(NSNgachBacLuong phucap)
        {
            List<NSNgachBacLuong> lst = (List<NSNgachBacLuong>)Session["NBL"];
            if (lst == null)
            {
                lst = new List<NSNgachBacLuong>();
            }
            lst.Add(phucap);
            Session["NBL"] = lst;
            storeNgachCongChuc.DataSource = lst;
            storeNgachCongChuc.DataBind();

        }
        public void GetNgoaiNgu(NSNgoaiNgu ngoaiNgu)
        {
            List<NSNgoaiNgu> lst = (List<NSNgoaiNgu>)Session["NgoaiNgu"];
            if (lst == null)
            {
                lst = new List<NSNgoaiNgu>();
            }
            lst.Add(ngoaiNgu);
            Session["NgoaiNgu"] = lst;
            storeNgoaiNgu.DataSource = lst;
            storeNgoaiNgu.DataBind();

        }
        public void GetKhenThuong(NSKhenThuong item)
        {
            List<NSKhenThuong> lst = (List<NSKhenThuong>)Session["KT"];
            if (lst == null)
            {
                lst = new List<NSKhenThuong>();
            }
            lst.Add(item);
            Session["KT"] = lst;
            storeKhenThuong.DataSource = lst;
            storeKhenThuong.DataBind();

        }
        public void GetKiLuat(NSKiLuat kiLuat)
        {
            List<NSKiLuat> lst = (List<NSKiLuat>)Session["KL"];
            if (lst == null)
            {
                lst = new List<NSKiLuat>();
            }
            lst.Add(kiLuat);
            Session["KL"] = lst;
            storeKiLuat.DataSource = lst;
            storeKiLuat.DataBind();

        }
        public void GetNhanXet(NSNhanXet nhanXet)
        {
            List<NSNhanXet> lst = (List<NSNhanXet>)Session["NX"];
            if (lst == null)
            {
                lst = new List<NSNhanXet>();
            }
            lst.Add(nhanXet);
            Session["NX"] = lst;
            storeNhanXet.DataSource = lst;
            storeNhanXet.DataBind();
        }
        public void GetTrinhDoChuyenMon(NSTrinhDoChuyenMon nhanXet)
        {
            List<NSTrinhDoChuyenMon> lst = (List<NSTrinhDoChuyenMon>)Session["TDCM"];
            if (lst == null)
            {
                lst = new List<NSTrinhDoChuyenMon>();
            }
            lst.Add(nhanXet);
            Session["TDCM"] = lst;
            storeTrinhDoChuyenMon.DataSource = lst;
            storeTrinhDoChuyenMon.DataBind();
        }
        public void GetNgheNghiep(NSNgheNghiep nghe)
        {
            List<NSNgheNghiep> lst = (List<NSNgheNghiep>)Session["NgheNghiep"];
            if (lst == null)
            {
                lst = new List<NSNgheNghiep>();
            }
            lst.Add(nghe);
            Session["NgheNghiep"] = lst;
            storeNgheNghiep.DataSource = lst;
            storeNgheNghiep.DataBind();

        }
        public void GetQTCT(NSQuaTrinhCongTac item)
        {
            List<NSQuaTrinhCongTac> lst = (List<NSQuaTrinhCongTac>)Session["QTCT"];
            if (lst == null)
            {
                lst = new List<NSQuaTrinhCongTac>();
            }
            lst.Add(item);
            Session["QTCT"] = lst;
            storeQTCT.DataSource = lst;
            storeQTCT.DataBind();

        }
        public void GetLiLuanChinhTri(NSLiLuanChinhTri item)
        {
            List<NSLiLuanChinhTri> lst = (List<NSLiLuanChinhTri>)Session["LiLuan"];
            if (lst == null)
            {
                lst = new List<NSLiLuanChinhTri>();
            }
            lst.Add(item);
            Session["LiLuan"] = lst;
            storeLiLuanChinhTri.DataSource = lst;
            storeLiLuanChinhTri.DataBind();

        }
        public void GetQuaTrinhCongTacDang(NSQuaTrinhCongTacDang item)
        {
            List<NSQuaTrinhCongTacDang> lst = (List<NSQuaTrinhCongTacDang>)Session["QuaTrinhCongTacDang"];
            if (lst == null)
            {
                lst = new List<NSQuaTrinhCongTacDang>();
            }
            lst.Add(item);
            Session["QuaTrinhCongTacDang"] = lst;
            storeQuaTrinhCongTacDang.DataSource = lst;
            storeQuaTrinhCongTacDang.DataBind();

        }
        public void GetDT(NSDaoTao item)
        {
            List<NSDaoTao> lst = (List<NSDaoTao>)Session["DT"];
            if (lst == null)
            {
                lst = new List<NSDaoTao>();
            }
            lst.Add(item);
            Session["DT"] = lst;
            storeDaoTao.DataSource = lst;
            storeDaoTao.DataBind();

        }
        public void GetTrangThaiLamViec(NSTrangThaiLamViec item)
        {
            List<NSTrangThaiLamViec> lst = (List<NSTrangThaiLamViec>)Session["TrangThaiLamViec"];
            if (lst == null)
            {
                lst = new List<NSTrangThaiLamViec>();
            }
            lst.Add(item);
            Session["TrangThaiLamViec"] = lst;
            storeTrangThaiLamViec.DataSource = lst;
            storeTrangThaiLamViec.DataBind();

        }
        public void GetBangCap(NSBangCap item)
        {
            List<NSBangCap> lst = (List<NSBangCap>)Session["BangCap"];
            if (lst == null)
            {
                lst = new List<NSBangCap>();
            }
            lst.Add(item);
            Session["BangCap"] = lst;
            storeBangCap.DataSource = lst;
            storeBangCap.DataBind();
        }
        public void GetQH(NSQuanHeGiaDinh item, string typeQuanHe)
        {
            List<NSQuanHeGiaDinh> lst = new List<NSQuanHeGiaDinh>();
            if (typeQuanHe.Equals("QHBT"))
            {
                lst = (List<NSQuanHeGiaDinh>)Session["QHBT"];
                if (lst == null)
                {
                    lst = new List<NSQuanHeGiaDinh>();
                }
                lst.Add(item);
                Session["QHBT"] = lst;
                storeQuanHeBanThan.DataSource = lst;
                storeQuanHeBanThan.DataBind();
            }
            else
            {
                lst = (List<NSQuanHeGiaDinh>)Session["QH"];
                if (lst == null)
                {
                    lst = new List<NSQuanHeGiaDinh>();
                }
                lst.Add(item);
                Session["QH"] = lst;
                storeQuanHeBenVo.DataSource = lst;
                storeQuanHeBenVo.DataBind();
            }
        }
        #endregion

        #region Load lại các grid
        public void ReloadGrid(string type)
        {
            if (type.Equals("HDLD"))
            {
                List<NSHopDongLaoDong> lst = (List<NSHopDongLaoDong>)Session[type];
                storeHopDongLaoDong.DataSource = lst;
                storeHopDongLaoDong.DataBind();
            }
            else if (type.Equals("PCCV"))
            {
                List<NSPhuCapChucVu> lst = (List<NSPhuCapChucVu>)Session[type];
                storePhuCapChucVu.DataSource = lst;
                storePhuCapChucVu.DataBind();
            }
            else if (type.Equals("PCK"))
            {
                List<NSPhuCapKhac> lstPCK = (List<NSPhuCapKhac>)Session[type];
                storePhuCapKhac.DataSource = lstPCK;
                storePhuCapKhac.DataBind();
            }
            else if (type.Equals("TrangThaiLamViec"))
            {
                List<NSTrangThaiLamViec> lstPCK = (List<NSTrangThaiLamViec>)Session[type];
                storeTrangThaiLamViec.DataSource = lstPCK;
                storeTrangThaiLamViec.DataBind();
            }
            else if (type.Equals("NBL"))
            {
                List<NSNgachBacLuong> lstPCK = (List<NSNgachBacLuong>)Session[type];
                storeNgachCongChuc.DataSource = lstPCK;
                storeNgachCongChuc.DataBind();
            }
            else if (type.Equals("NgoaiNgu"))
            {
                List<NSNgoaiNgu> lstPCK = (List<NSNgoaiNgu>)Session[type];
                storeNgoaiNgu.DataSource = lstPCK;
                storeNgoaiNgu.DataBind();
            }
            else if (type.Equals("NghiPhep"))
            {
                List<NSNghiPhep> lstPCK = (List<NSNghiPhep>)Session[type];
                storeNghiPhep.DataSource = lstPCK;
                storeNghiPhep.DataBind();
            }
            else if (type.Equals("QTGD"))
            {
                List<NSQuaTrinhGianDoanBaoHiem> lstPCK = (List<NSQuaTrinhGianDoanBaoHiem>)Session[type];
                storeQTGDBaoHiem.DataSource = lstPCK;
                storeQTGDBaoHiem.DataBind();
            }
            else if (type.Equals("KT"))
            {
                List<NSKhenThuong> lstPCK = (List<NSKhenThuong>)Session[type];
                storeKhenThuong.DataSource = lstPCK;
                storeKhenThuong.DataBind();
            }
            else if (type.Equals("KL"))
            {
                List<NSKiLuat> lstPCK = (List<NSKiLuat>)Session[type];
                storeKiLuat.DataSource = lstPCK;
                storeKiLuat.DataBind();
            }
            else if (type.Equals("NX"))
            {
                List<NSNhanXet> lstPCK = (List<NSNhanXet>)Session[type];
                storeNhanXet.DataSource = lstPCK;
                storeNhanXet.DataBind();
            }
            else if (type.Equals("QTCT"))
            {
                List<NSQuaTrinhCongTac> lstPCK = (List<NSQuaTrinhCongTac>)Session[type];
                storeQTCT.DataSource = lstPCK;
                storeQTCT.DataBind();
            }

            else if (type.Equals("QuaTrinhCongTacDang"))
            {
                List<NSQuaTrinhCongTacDang> lstPCK = (List<NSQuaTrinhCongTacDang>)Session[type];
                storeQuaTrinhCongTacDang.DataSource = lstPCK;
                storeQuaTrinhCongTacDang.DataBind();
            }
            else if (type.Equals("DT"))
            {
                List<NSDaoTao> lstPCK = (List<NSDaoTao>)Session[type];
                storeDaoTao.DataSource = lstPCK;
                storeDaoTao.DataBind();
            }
            else if (type.Equals("NgheNghiep"))
            {
                List<NSNgheNghiep> lstPCK = (List<NSNgheNghiep>)Session[type];
                storeNgheNghiep.DataSource = lstPCK;
                storeNgheNghiep.DataBind();
            }
            else if (type.Equals("TDCM"))
            {
                List<NSTrinhDoChuyenMon> lstPCK = (List<NSTrinhDoChuyenMon>)Session[type];
                storeTrinhDoChuyenMon.DataSource = lstPCK;
                storeTrinhDoChuyenMon.DataBind();
            }
            else if (type.Equals("BangCap"))
            {
                List<NSBangCap> lst = (List<NSBangCap>)Session[type];
                storeBangCap.DataSource = lst;
                storeBangCap.DataBind();
            }
            else if (type.Equals("LiLuan"))
            {
                List<NSLiLuanChinhTri> lst = (List<NSLiLuanChinhTri>)Session[type];
                storeLiLuanChinhTri.DataSource = lst;
                storeLiLuanChinhTri.DataBind();
            }
            else if (type.Equals("QLNN"))
            {
                List<NSQuanLiNhaNuoc> lst = (List<NSQuanLiNhaNuoc>)Session[type];
                storeQuanLiNhaNuoc.DataSource = lst;
                storeQuanLiNhaNuoc.DataBind();
            }
            else if (type.Equals("TDTH"))
            {
                List<NSTrinhDoTinHoc> lst = (List<NSTrinhDoTinHoc>)Session[type];
                storeTrinhDoTinHoc.DataSource = lst;
                storeTrinhDoTinHoc.DataBind();
            }
        }
        public void ReloadGridQuanHe(string typeQuanHe)
        {
            if (typeQuanHe.Equals("QHBT"))
            {
                List<NSQuanHeGiaDinh> lstPCK = (List<NSQuanHeGiaDinh>)Session[typeQuanHe];
                storeQuanHeBanThan.DataSource = lstPCK;
                storeQuanHeBanThan.DataBind();
            }
            else if (typeQuanHe.Equals("QH"))
            {
                List<NSQuanHeGiaDinh> lstPCK = (List<NSQuanHeGiaDinh>)Session[typeQuanHe];
                storeQuanHeBenVo.DataSource = lstPCK;
                storeQuanHeBenVo.DataBind();
            }
        }
        #endregion

        [DirectMethod]
        public void LoadLookUpToChuc()
        {
            bool flag = !string.IsNullOrEmpty(hdFlagToChuc.Value.ToString()) ? Convert.ToBoolean(hdFlagToChuc.Value.ToString()) : true;
            UcToChuc.ShowLookUp_ToChuc("ToChucKhac", flag, txtCoQuanTuyenDung.Text);
            X.Mask.Hide();
        }

        [DirectMethod]
        public void LoadLookUpToChucForToChucQuanLy()
        {
            UcToChuc.ShowLookUp_ToChuc("", false, "");
            X.Mask.Hide();
        }
    }
}